<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "tally_arap".
 *
 * @property int $id 应收应付记录ID
 * @property int $uid 创建者
 * @property int $tally_id 账本id
 * @property int $category_id 类别ID
 * @property string $category_name 类别
 * @property int $category_pid
 * @property string $category_pname
 * @property int $account_id 账户ID
 * @property string $account_name 账户
 * @property int $account_dst_id 目标账户ID
 * @property string $account_dst_name 目标账户
 * @property int $project_id 项目ID
 * @property string $project_name 项目
 * @property int $store_id 商家ID
 * @property string $store_name 商家
 * @property int $member_id 成员ID
 * @property string $member_name 成员
 * @property int $type 账目类型10收入20支出30转账
 * @property string $amount 应收应付金额
 * @property string $amount_remain 应收应付余额
 * @property string $memo 备注
 * @property string $tally_time 账单时间
 * @property string $audit_time 审核时间
 * @property string $created_time 创建时间
 * @property string $updated_time 更新时间
 * @property int $state 记录状态1正常0待审
 */
class TallyArap extends \yii\db\ActiveRecord
{
    /**
     * 记录状态
     */
    const STATE_INVALID = 0; //状态等待审核
    const STATE_VALID   = 1; //状态正常 待核销
    const STATE_ARAP_ING   = 2; //状态核销中
    const STATE_ARAP_OVER   = 3; //状态核销完毕
    const STATE_REFUSED = -1; //状态拒绝 审核未通过

    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'tally_arap';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['uid', 'tally_id', 'category_id', 'category_pid', 'account_id', 'account_dst_id', 'project_id', 'store_id', 'member_id', 'type', 'state'], 'integer'],
            [['amount', 'amount_remain'], 'number'],
            [['tally_time', 'audit_time', 'created_time', 'updated_time'], 'safe'],
            [['material_json'],'string'],
            [['category_name', 'category_pname', 'account_name', 'account_dst_name', 'project_name', 'store_name', 'member_name'], 'string', 'max' => 60],
            [['memo'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'uid' => 'Uid',
            'tally_id' => 'Tally ID',
            'category_id' => 'Category ID',
            'category_name' => 'Category Name',
            'category_pid' => 'Category Pid',
            'category_pname' => 'Category Pname',
            'account_id' => 'Account ID',
            'account_name' => 'Account Name',
            'account_dst_id' => 'Account Dst ID',
            'account_dst_name' => 'Account Dst Name',
            'project_id' => 'Project ID',
            'project_name' => 'Project Name',
            'store_id' => 'Store ID',
            'store_name' => 'Store Name',
            'member_id' => 'Member ID',
            'member_name' => 'Member Name',
            'type' => 'Type',
            'amount' => 'Amount',
            'amount_remain' => 'Amount Remain',
            'memo' => 'Memo',
            'tally_time' => 'Tally Time',
            'audit_time' => 'Audit Time',
            'created_time' => 'Created Time',
            'updated_time' => 'Updated Time',
            'state' => 'State',
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     * @notes 获取附件列表
     */
    public function getAttachment()
    {
        return $this->hasMany(TallyAttachment::className(),['tally_arap_id' => 'id']);
    }

    /**
     * @param $status
     *
     * @return string
     * @desc 获取状态文字
     */
    public static function getStatusTxt($status)
    {
        if($status == self::STATE_ARAP_OVER) {
            return '核销完毕';
        }else if($status == self::STATE_ARAP_ING) {
            return '核销中';
        }else if($status == self::STATE_VALID) {
            return '待核销';
        }else if($status == self::STATE_INVALID) {
            return '待审核';
        }else if($status == self::STATE_REFUSED) {
            return '已忽略';
        }else{
            return '未知';
        }
    }

    /**
     * @param $role
     *
     * @return int|null
     * @desc 不同角色默认不同账单状态
     */
    public static function getDefaultStateByRole($role)
    {
        $state = null ;
        if($role == TallyPm::ROLE_ADMIN) {
            $state = self::STATE_VALID;
        }else if($role == TallyPm::ROLE_FINANCE) {
            $state = self::STATE_INVALID;
        }else if($role == TallyPm::ROLE_ACCOUNTANT) {
            $state = self::STATE_INVALID;
        }else if($role == TallyPm::ROLE_CASHIER) {
            $state = self::STATE_VALID;
        }else if($role == TallyPm::ROLE_MANAGER) {
            $state = self::STATE_VALID;
        }
        return $state;
    }

}
